﻿using System;
using System.Data;

namespace AssettDatabase.DAL
{
    class Asset_Type
    {
        private string sqltext = "";

        public bool Add(string Asset_Type_Name)
        {
            try
            {
                DataAccess.DAO mDAO = new DataAccess.DAO();
                sqltext =   "INSERT INTO Asset_Type (Asset_Type_Name, Asset_Type_Status)" +
                            "VALUES (@Asset_Type_Name, 1);";

                mDAO.AddParameter("@Asset_Type_Name", Asset_Type_Name);
                mDAO.ExecuteNonQuery(sqltext);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return true;
        }

        public bool Update(string Asset_Type_Name, string Asset_Type_ID)
        {
            try
            {
                DataAccess.DAO mDAO = new DataAccess.DAO();
                sqltext =   "UPDATE Asset_Type SET Asset_Type_Name=@Asset_Type_Name," +
                            "WHERE Asset_Type_ID=@Asset_Type_ID";

                mDAO.AddParameter("@Asset_Type_Name", Asset_Type_Name);
                mDAO.AddParameter("@Asset_Type_ID", Asset_Type_ID);
                mDAO.ExecuteNonQuery(sqltext);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return true;
        }

        public DataTable GetAsset_Type()
        {
            try
            {
                DataAccess.DAO mDAO = new DataAccess.DAO();
                sqltext = "SELECT Asset_Type_ID,  " +
                            "CASE WHEN Asset_Type_Status = 1 THEN Asset_Type_Name ELSE CONCAT(Asset_Type_Name, ' (retired)') END AS Asset_Type_Name " +
                            "FROM Asset_Type " +
                            "ORDER BY Asset_Type_Name";

                return mDAO.ExecuteSelectQuery(sqltext);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
